import lodash from 'lodash'
import { JobAction, JobRecordModel } from './JobRecordModel'

/**
 * 排班判断模型
 */
export class ScheduleModel {
  private records: JobRecordModel[]

  constructor(records: JobRecordModel[]) {
    if (!records.length) {
      throw new Error('this.records.length === 0')
    }
    this.records = lodash.cloneDeep(records)
    // sort(asc) by time
    JobRecordModel.sort(this.records)
    // this.records.sort((a, b) => {
    //     const _d = (x: JobRecordModel) => {
    //         // 为了避免入职，离职，结清等事件难以排序。分别对其进行一定程度的处理
    //         if (x.action === JobAction.End) {
    //             return x.date.valueOf() + 10000
    //         }
    //         else if (x.action === JobAction.Settlement) {
    //             return x.date.valueOf() + 20000
    //         }
    //         else {
    //             return x.date.valueOf()
    //         }
    //     }
    //     return _d(a) - _d(b)
    // })
  }

  /** 工人 */
  worker() {
    return this.records[0].worker!
  }

  /** 是否已经离职 */
  left() {
    let res = false
    for (const record of this.records) {
      if (record.action === JobAction.End) {
        res = true
      }
      else if (record.action === JobAction.Start) {
        res = false
      }
    }

    return res
  }

  getSchedule() {
    let res = '-'
    for (const record of this.records) {
      if (record.action === JobAction.Start) {
        res = record.actionParams[1]
      }
      else if (record.action === JobAction.Schedule) {
        res = record.actionParams[0]
      }
      else if (record.action === JobAction.End) {
        res = '已离职'
      }
    }

    return res
  }
}
